package com.xingkeduo.pojo.json;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.table.CountryTable;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.tool.CommonUtil;

import java.util.Iterator;
import java.util.List;

public class CountryJson extends BaseJson {
	public static ObjectNode toJson(CountryTable countryTable) throws Throwable {
		ObjectNode root = MAPPER.createObjectNode();

		if (countryTable != null)
		{
			root.put("id", countryTable.getId());
			root.put("pid", countryTable.getPid());
			root.put("name", countryTable.getName());
			root.put("type", countryTable.getType());
			if(!CommonUtil.isEmpty(countryTable.getChildren())){
				root.put("children", toJson(countryTable.getChildren()));
			}
		}
		return root;
	}

	public static ArrayNode toJson(List<CountryTable> tables) throws Throwable {
		ArrayNode root = MAPPER.createArrayNode();

		if (!CommonUtil.isEmpty(tables))
			for (Iterator<CountryTable> iterator = tables.iterator(); iterator.hasNext();) {
				root.add(toJson(iterator.next()));
			}
		return root;
	}

}
